package com.qtt.app.service.impl;

import com.qtt.app.entity.*;
import com.qtt.app.entity.ReturnInfo.ReturnTeacherInfo;
import com.qtt.app.mapper.AppInstitutionMapper;
import com.qtt.app.mapper.AppTeacherMapper;
import com.qtt.app.mapper.TeachersMapper;
import com.qtt.app.service.AppTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AppTeacherServiceImpl implements AppTeacherService {
    @Autowired
    AppTeacherMapper appTeacherMapper;
    @Autowired
    TeachersMapper teachersMapper;
    @Autowired
    AppInstitutionMapper appInstitutionMapper;

    @Override
    public List<ReturnTeacherInfo> getInstitutionTeacherByInstId(Integer institutionId) {
        List<ReturnTeacherInfo> returnTeacherInfoEntities = new ArrayList<ReturnTeacherInfo>();
        //获取到我自己拓展创建的appcourse表，接下来遍历它。
        List<AppTeacher> appTeacherEntities = appTeacherMapper.getInstitutionTeacher(institutionId);
        for(int i = 0 ; i < appTeacherEntities.size() ; i++) {
            System.out.println(appTeacherEntities.get(i));
            AppTeacher appTeacherEntity ;//编译器提示=new AppCourse()是多余的。
            ReturnTeacherInfo returnTeacherInfoEntity = new ReturnTeacherInfo();
            //第一步：整合appTeacher的数据过来,该表仅含有一个字段
            appTeacherEntity = appTeacherEntities.get(i);
            returnTeacherInfoEntity.setInstitutionId(institutionId);
            returnTeacherInfoEntity.setId(appTeacherEntity.getId());
            returnTeacherInfoEntity.setTeacherId( appTeacherEntity.getTeacherId());

            //第二步：整合teacher的数据过来，该表中以teacherId为主键
            Teachers teachersEntity = teachersMapper.selectByPrimaryKey(appTeacherEntity.getTeacherId());
            returnTeacherInfoEntity.setRealName(teachersEntity.getRealName());
            returnTeacherInfoEntity.setRanks(teachersEntity.getRanks());
            returnTeacherInfoEntity.setTeacherPicture(teachersEntity.getHead());
            returnTeacherInfoEntity.setTeacherGrade(teachersEntity.getGrade());
            returnTeacherInfoEntity.setIntroduction(teachersEntity.getIntroduction());
            returnTeacherInfoEntity.setCourseNum(teachersEntity.getCourseNum());
            returnTeacherInfoEntity.setTeachNum(teachersEntity.getTeachNum());

//            //第三步，整合app_institution表.institution_id为主键，可直接采用自带的主键查找。
//            AppInstitution appInstitutionEntity = appInstitutionMapper.selectByPrimaryKey(appTeacherEntity.getInstitutionId());
//            returnAppCourseInfoEntity.setInstitutionName(appInstitutionEntity.getInstitutionName());
//            returnAppCourseInfoEntity.setLogoPicture(appInstitutionEntity.getLogoPicture());
//            returnAppCourseInfoEntity.setInstitutionIntroduction(appInstitutionEntity.getIntroduction());

            //最后，添加该entity到list集合中
            returnTeacherInfoEntities.add(returnTeacherInfoEntity);
        }
        return returnTeacherInfoEntities;
    }

    @Override
    public int addTeacher(Teachers teachersEntity) {
        int flag = teachersMapper.insert(teachersEntity);
        int id;
        if(flag == 1){
            id = teachersMapper.getLastId();//返回添加teacher记录的id
            System.out.println("ID="+id);
        }
        else{
            id = 0;
        }
        return id;
    }

    @Override
    public int addAppTeacher(int teacherId, Integer institutionId) {
        AppTeacher appTeacherEntity = new AppTeacher();
        Integer teacherId2 = new Integer(teacherId);
        appTeacherEntity.setInstitutionId(institutionId);
        appTeacherEntity.setTeacherId(teacherId2);
        int flag = appTeacherMapper.insert(appTeacherEntity);
        if(flag!=0){return 200;}//成功
        else{return 400;}
    }

    @Override
    public List<Teachers> getRecommendTeachers() {
        List<Teachers> teacherEntities = teachersMapper.getRecommendTeachers();
        return teacherEntities;
    }
}
